<?
require("config.php");

if (isset($_POST["keyword"]) || isset($_GET["keyword"])) {

$page = isset($_GET["page"]) ? $_GET["page"] : 1;

if (isset($_POST["keyword"]))
$keyword = $_POST["keyword"];
else
$keyword = $_GET["keyword"];

if (!isset($_GET["page"])) {

$testq = "select id, access from med_keyword where keyword='$keyword'";
$testr = myQuery($testq);
if (empty($testr[0][0])) {

   $field = array(
	"keyword",
	"access",
	);

	$content = array(
	$keyword,
	1,
	);
	
	myInsert("med_keyword",$field,$content);

} else {

$q = "update med_keyword set access=access+1 where id='". $testr[0][0] . "'";
$r = myQuery($q);

}


}




$aboutusq = "select id from med_display_content where code='aboutus' and details like '%$keyword%'";
$aboutusr = myQuery($aboutusq);

//echo "aboutus: " . count($aboutus). "<br>";

$ourtrainq = "select id from med_display_content where code='ourtraining' and details like '%$keyword%'";
$ourtrainr = myQuery($ourtrainq);

//echo "ourtrainr: " . count($ourtrainr). "<br>";

$aeveningq = "select id, presented from med_aevening where presented like '%$keyword%' or date like '%$keyword%' or time like '%$keyword%' or contact like '%$keyword%' or name like '%$keyword%'or venue like '%$keyword%'";
$aeveningr = myQuery($aeveningq);

//echo "aeveningr: " . count($aeveningr). "<br>";

$asharingq = "select id, graduate, schedule from course_sharing where graduate like '%$keyword%' or detail like '%$keyword%' or schedule like '%$keyword%'";
$asharingr = myQuery($asharingq);

$scheduleid = array();

for ($i=0;$i<count($asharingr);$i++) {
	$scheduleq = "select id from course_schedule where name='".$asharingr[$i][2]."' And status='yes'";
	$scheduler = myQuery($scheduleq);
	$scheduleid[$i] = $scheduler[0][0];
}

//echo "asharingr: " . count($asharingr). "<br>";

$trainerq = "select id, name from course_trainer_info where name like '%$keyword%' or brief like '%$keyword%'";
$trainerr = myQuery($trainerq);

//echo "trainerr: " . count($trainerr). "<br>";

$aboardq = "select id, subject from med_aboard where subject like '%$keyword%' or details like '%$keyword%'";
$aboardr = myQuery($aboardq);

//echo "aboardr: " . count($aboardr). "<br>";

$newsq = "select id, date, subject from med_news where (date like '%$keyword%' or subject like '%$keyword%' or details like '%$keyword%') and (status='Yes')";
$newsr = myQuery($newsq);

//echo "newsr: " . count($newsr). "<br>";

$newsletterq = "select id, date, subject from med_newsletter where date like '%$keyword%' or subject like '%$keyword%' or details like '%$keyword%'";
$newsletterr = myQuery($newsletterq);

//echo "newsletterr: " . count($newsletterr). "<br>";

$linkq = "select pid, name from link where name like '%$keyword%' or description like '%$keyword%' or url like '%$keyword%'";
$linkr = myQuery($linkq);

//echo "linkr: " . count($linkr). "<br>";

$result = array();
$rcount = 0;


if (!empty($aboutusr[0][0])) {
	$result[$rcount][0] = "About Us";
	$result[$rcount][1] = "aboutus.php";
	$rcount++;
}

if (!empty($ourtrainr[0][0])) {
	$result[$rcount][0] = "Our Training";
	$result[$rcount][1] = "course.php";
	$rcount++;
}

if (!empty($aeveningr[0][0])) {
	for ($i=0;$i<count($aeveningr);$i++) {
		$result[$rcount][0] = $aeveningr[$i][1];
		$result[$rcount][1] = "aevening.php";
		$rcount++;
	}
}

if (!empty($asharingr[0][0])) {
	for ($i=0;$i<count($asharingr);$i++) {
		if (!empty($scheduleid[$i])) {
		$result[$rcount][0] = $asharingr[$i][1];
		$result[$rcount][1] = "graduate_sharing.php?id=". $scheduleid[$i];
		$rcount++;
		}
	}
}

if (!empty($trainerr[0][0])) {
	for ($i=0;$i<count($trainerr);$i++) {
		$result[$rcount][0] = $trainerr[$i][1];
		$result[$rcount][1] = "trainer_detail.php?id=". $trainerr[$i][0];
		$rcount++;
	}
}

if (!empty($aboardr[0][0])) {
	for ($i=0;$i<count($aboardr);$i++) {
		$result[$rcount][0] = $aboardr[$i][1];
		$result[$rcount][1] = "adetail.php?id=". $aboardr[$i][0];
		$rcount++;
	}
}

if (!empty($newsr[0][0])) {
	for ($i=0;$i<count($newsr);$i++) {
		$result[$rcount][0] = $newsr[$i][1] . " - " . $newsr[$i][2];
		$result[$rcount][1] = "news.php?id=". $newsr[$i][0];
		$rcount++;
	}
}

if (!empty($newsletterr[0][0])) {
	for ($i=0;$i<count($newsletterr);$i++) {
		$result[$rcount][0] = $newsletterr[$i][1] . " - " . $newsletterr[$i][2];
		$result[$rcount][1] = "newsletters.php?id=". $newsletterr[$i][0];
		$rcount++;
	}
}

if (!empty($linkr[0][0])) {
	for ($i=0;$i<count($linkr);$i++) {
		$result[$rcount][0] = $linkr[$i][1];
		$result[$rcount][1] = "link_detail.php?id=". $linkr[$i][0];
		$rcount++;
	}
}

$num_rows = count($result);

$psize = 15;
$pages = intval($num_rows / $psize);

if ($num_rows % $psize) $pages++;
if (empty($page) || ($page < 1))
{
  $page = 1;
  $offset = 0;
}elseif ($page > $pages)
{
  $page = $pages;
  $offset = $page * $psize - $psize;
}
else
{
  $offset = $page * $psize - $psize;
}
$totalpage = ceil($num_rows/$psize);
if ($page == 1) { $from = 1; if ($totalpage > 1) $to = 5;else $to = $num_rows; } 
elseif ($page < $totalpage) { $to = $psize*$page; $from = $psize*($page-1)+1; }
else { $to = $psize*($page-1)+$num_rows%$psize; $from = $psize*($page-1)+1; }


}



require("header.php");
?>



<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr> 

    <td width="186" valign="top"> 
      <?require("nav.php")?>
    </td>
    <td width="11"><img src="images/shim.gif" width="11" height="1"></td>
    <td valign="top"> 
      <?require("menu.php")?>
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr> 
          <td align="right"><table width="100%" border="0">
              <tr> 
                <td height="50" class="header">&gt;&gt; Search Result </td>
              </tr>
            </table></td>
        </tr>
      </table>
      <table width="95%" border="0" cellspacing="0" cellpadding="1" align="center">
        <tr> 
          <td> <p><B><?=$num_rows?> references found for the search term "<?=$keyword?>".</B> <BR>
            Displaying page <?=$page?> of <?=$totalpage?>: </p>
            <table width="100%"  border="0" cellspacing="0" cellpadding="2">
						
						<?php 
							for ($i=$offset;$i < ($offset + $psize); $i++) {
							
						?>
              <tr>
                <td width="5%">&nbsp;</td>
                <td width="95%"><a href="<?=$result[$i][1]?>"><?=$result[$i][0]?></a></td>
              </tr>
              <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
           <?php } ?>
            </table>
						<p>
            <?php 
							//$level2 = $level++;
							$tak = multi($num_rows, $psize, $page, "search.php?keyword=$keyword");
echo $tak;
?></p>
          </td>
        </tr>
      </table>
      <br>
      <br> </td>
    <td width="11"><img src="images/shim.gif" width="11" height="1"></td>
    <td width="173" valign="top">
	
	
<?require("list.php")?>	 
	  
	  
    </td>

  </tr>
</table>





<?require("footer.php")?>	